package com.ruoyi.cloudpc.controller;

import com.ruoyi.cloudpc.domain.CloudPromoterApprove;
import com.ruoyi.cloudpc.service.ICloudPromoterApproveService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 申请推广人Controller
 *
 * @author Ten
 * @date 2025-10-09
 */
@RestController
@RequestMapping("/cloudpc/promoterapprove")
public class CloudPromoterApproveController extends BaseController {
    @Autowired
    private ICloudPromoterApproveService cloudPromoterApproveService;

    /**
     * 查询申请推广人列表
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:list')")
    @GetMapping("/list")
    public TableDataInfo list(CloudPromoterApprove cloudPromoterApprove) {
        startPage();
        List<CloudPromoterApprove> list = cloudPromoterApproveService.selectCloudPromoterApproveList(cloudPromoterApprove);
        return getDataTable(list);
    }

    /**
     * 导出申请推广人列表
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:export')")
    @Log(title = "申请推广人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CloudPromoterApprove cloudPromoterApprove) {
        List<CloudPromoterApprove> list = cloudPromoterApproveService.selectCloudPromoterApproveList(cloudPromoterApprove);
        ExcelUtil<CloudPromoterApprove> util = new ExcelUtil<CloudPromoterApprove>(CloudPromoterApprove.class);
        util.exportExcel(response, list, "申请推广人数据");
    }

    /**
     * 获取申请推广人详细信息
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(cloudPromoterApproveService.selectCloudPromoterApproveByUserId(userId));
    }

    /**
     * 新增申请推广人
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:add')")
    @Log(title = "申请推广人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CloudPromoterApprove cloudPromoterApprove) {
        return toAjax(cloudPromoterApproveService.insertCloudPromoterApprove(cloudPromoterApprove));
    }

    /**
     * 修改申请推广人
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:edit')")
    @Log(title = "申请推广人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CloudPromoterApprove cloudPromoterApprove) {
        return toAjax(cloudPromoterApproveService.updateCloudPromoterApprove(cloudPromoterApprove));
    }

    /**
     * 删除申请推广人
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:remove')")
    @Log(title = "申请推广人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(cloudPromoterApproveService.deleteCloudPromoterApproveByUserIds(userIds));
    }

    /**
     * 我的申请
     * @param cloudPromoterApprove
     * @return
     */
    @GetMapping("/mylist")
    public TableDataInfo mylist(CloudPromoterApprove cloudPromoterApprove) {
        cloudPromoterApprove.setCreateByAndTime();
        startPage();
        List<CloudPromoterApprove> list = cloudPromoterApproveService.selectCloudPromoterApproveList(cloudPromoterApprove);
        return getDataTable(list);
    }

    /**
     * 管理列表/审批列表
     * @param cloudPromoterApprove
     * @return
     */
    @GetMapping("/managelist")
    public TableDataInfo managelist(CloudPromoterApprove cloudPromoterApprove) {
        startPage();
        List<CloudPromoterApprove> list = cloudPromoterApproveService.selectCloudPromoterApproveList(cloudPromoterApprove);
        return getDataTable(list);
    }

    /**
     * 审批
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:promoterapprove:edit')")
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody CloudPromoterApprove cloudPromoterApprove) {
        cloudPromoterApprove.setApprover(SecurityUtils.getUserId());
        cloudPromoterApprove.setApproveDate(DateUtils.getNowDate());
        return toAjax(cloudPromoterApproveService.approve(cloudPromoterApprove));
    }

}
